package com.example.myBlogTest.service;

import com.example.myBlogTest.entity.Blog;
import com.example.myBlogTest.exception.NotFoundException;
import com.example.myBlogTest.repository.BlogRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author: Lucy
 * Description:
 */
@Service
public class BlogService {

    private BlogRepository blogRepository;

    public BlogService(BlogRepository blogRepository) {
        this.blogRepository = blogRepository;
    }

    public Page<Blog> getBlogsByPage(int currentPage, int pageSize) {
        Page<Blog> blogs = blogRepository.findAll(PageRequest.of(currentPage-1, pageSize));
        return blogs;
    }

    public Blog findById(Long id) {
        return blogRepository.findById(id).orElseThrow(NotFoundException::new);
    }

    public void saveOrUpdate(Blog tempBlog) {
        blogRepository.save(tempBlog);
    }
}
